<div id="page_joke">
<?php
//Variable pour la gestion des pages
if( !isset($_GET['page']) OR ! is_numeric($_GET['page'])){
	$page = 1;
}else{
	$page = mysql_escape_string($_GET['page']);
}
$nbjokepage = 10;
$nbjoke = Joke::getNumberJoke();
$nbpage = ceil($nbjoke/$nbjokepage);
$firstjoke = ($page * $nbjokepage) -$nbjokepage;
//end of variables

//gestion de la requete selon demande
if(isset($_GET['sort']) and $_GET['sort']!=''){
	if($_GET['sort']=='latest'){
		$mysql_query = mysql_query("SELECT id,date,joke,score FROM ".DATABASE_TABLE_JOKE."
							 ORDER BY date DESC LIMIT $firstjoke,$nbjokepage;" );
	}elseif($_GET['sort']=='top'){
		$mysql_query = mysql_query("SELECT id,date,joke,score FROM ".DATABASE_TABLE_JOKE."
							 ORDER BY score DESC LIMIT $firstjoke,$nbjokepage;" );
	}elseif($_GET['sort']=='flop'){
		$mysql_query = mysql_query("SELECT id,date,joke,score FROM ".DATABASE_TABLE_JOKE."
							 ORDER BY score ASC LIMIT $firstjoke,$nbjokepage;" );
	}
}else{
	$mysql_query = mysql_query("SELECT id,date,joke,score FROM ".DATABASE_TABLE_JOKE."
							 ORDER BY date DESC LIMIT $firstjoke,$nbjokepage;" );
}

?>



<!-- PAGE -->
<p class="page">Pages [ <?php
$i = 1;
while ($i != $nbpage+1){
?>
<a <?php if($i==$page){print 'style="font-weight:bold;"';}?> href = " index.php?page=<?php print $i; ?>&sort=<?php print $_GET['sort']; ?>" ><?php print $i ?></a>&nbsp;
<?php
$i++;
}
?> ]</p>
<!-- End Page -->



<?php 

while($query = mysql_fetch_array($mysql_query)){
?>
<div class="joke">
	<p>
		<?php print stripslashes(nl2br($query['joke'])); ?>
	</p>
	<p>
		n°<?php print $query['id']."  "; print date("d/m/y  G\hi\m",$query['date']); ?>
		score <?php print $query['score']; ?>
		<?php if($user->vote==1 and !Joke::userVote($query['id'],$user)){?>
		<span>(<span class='vote' onclick="ajax_vote(<?php print $query['id']?>,+1);this.parentNode.innerHTML='';">+</span> | <span class='vote' onclick="ajax_vote(<?php print $query['id']?>,-1);this.parentNode.innerHTML='';" >-</span>)</span>
		<?php }?>
		
	</p>
</div>



<!-- PAGE -->
<?php } ?>
<p class="page">Pages [ <?php 
$i = 1;
while ($i != $nbpage+1){
?>
<a <?php if($i==$page){print 'style="font-weight:bold;"';}?> href = " index.php?page=<?php print $i; ?>&sort=<?php print $_GET['sort']; ?>" ><?php print $i ?></a>&nbsp;
<?php
$i++;
}
?> ]</p>
<!-- End Page -->


</div>